﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationTest
{
    /// <summary>
    /// 负责对通知信息进行过滤，并记录至对应的数据表中
    /// </summary>
    public partial class Notify : System.Web.UI.Page
    {
        String TAB = "Notify表";
        String infoURL = "http://118.25.40.47/WebInfo.aspx";

        /// <summary>
        /// 获取请求参数信息
        /// </summary>
        private String getParam(String LogName = "")
        {
            String Url = Request.Url.ToString();
            String param = "";
            Url = System.Web.HttpUtility.UrlDecode(Url);                    // 解码参数
            if (Url.Contains("?"))
            {
                param = Url.Substring(Url.IndexOf("?") + 1);                // 获取参数信息

                if (LogName.Equals("")) LogName = this.GetType().Name;
                LogTool log = new LogTool(LogName);                         // 记录至log中
                log.WriteLine(param);
            }
            return param;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            infoURL = "http://" + Request.Params.Get("HTTP_HOST") + "/WebInfo.aspx";    // 本地表信息路径

            String param = getParam();                                  // 获取通知消息
            //param = "param=com.sc.notificationservices#866170022772077#1518361220871#标题#内容";
            if (param.StartsWith("param=")) param = param.Substring("param=".Length);

            if (!param.Equals("") && param.Contains("#"))
            {
                TAB = getTableName(param);              // 生成表名
                String key = getPhoneSerial(param);     // 获取手机序列号
                String result = SaveInfo(key, param);   // 通知信息保存结果

                Response.Write(result);
            }
            else
            {
                Response.Write("fail");
            }
        }

        /// <summary>
        /// 获取手机序列号信息，识别手机
        /// </summary>
        private String getPhoneSerial(String param)
        {
            int start = param.IndexOf("#") + 1;
            int end = param.IndexOf("#", start);
            String Serial = param.Substring(start, end - start);
            return Serial;
        }

        /// <summary>
        /// 获取通知消息的应用包名信息，生成对应的表
        /// </summary>
        private String getTableName(String param)
        {
            int start = param.IndexOf("#");
            String Name = param.Substring(0,start).Replace(".", "_");
            return Name;
        }
        
        /// <summary>
        /// 保存信息
        /// </summary>
        public String SaveInfo(String key, String vlaue)
        {
            vlaue = System.Web.HttpUtility.UrlEncode(vlaue);    //  数据信息中若含有特定字符，需要转码
            key = System.Web.HttpUtility.UrlEncode(key);

            String url = infoURL + "?" + "TAB=" + TAB + "&" + "KEY=" + key + "&" + "VALUE=" + vlaue;
            String data = getWebData(url);
            return data;
        }

        /// <summary>
        /// 获取指定url的网页数据
        /// </summary>
        public static String getWebData(String url)
        {
            //url = System.Web.HttpUtility.UrlEncode(url);
            String data = "";

            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            data = client.DownloadString(url);

            return data;
        }
    }
}